<?php

namespace apps\marriage\app\mp\controller;

use apps\marriage\core\service\MemberProfileService;
use apps\marriage\core\service\UserService;
use think\Exception;
use think\Request;

class MyMemberProfileController extends BaseMarriageMpController
{
    /**
     * @var MemberProfileService
     */
    private $memberProfileService;

    /**
     * @var UserService
     */
    private $userService;

    protected function init()
    {
        parent::init();
        $this->memberProfileService = service('MemberProfile', SERVICE_NAMESPACE);
        $this->userService = service('User', SERVICE_NAMESPACE);
    }

    public function modify_user_avatar(Request $request)
    {
        $avatar = $request->param('avatar', 0, 'intval');
        if($avatar == 0) {
            throw new Exception('请上传头像');
        }
        $this->memberProfileService->modifyUserAvatar($this->user['id'], $avatar);
        return $this->success();
    }

    public function modify_user_nick(Request $request)
    {
        $nick = $request->param('nick');
        $this->userService->updateByPk([
            'id' => $this->user['id'],
            'nick' => $nick
        ]);
        return $this->success();
    }

    public function my()
    {
        $member = $this->memberProfileService->getByUserId($this->user['id']);
        $member = $this->memberProfileService->format($member);
        $user = $this->userService->getByPk($this->user['id']);
        if($user['auditing_avatar'] > 0) {
            $user['auditing_avatar_url'] = $this->userService->getAvatarUrl($user['auditing_avatar'], $user['id']);
        }
        return $this->success([
            'member' => $member,
            'user' => $user
        ]);
    }

    public function save_member_photo(Request $request)
    {
        $photo = $request->param('photo', 0, 'intval');
        if($photo == 0) {
            throw new Exception('请上传图片');
        }
        $this->memberProfileService->modifyPhoto($this->user['id'], $photo);
        $member = $this->memberProfileService->getByUserId($this->user['id']);
        $member = $this->memberProfileService->format($member);
        return $this->success(['member' => $member]);
    }

    public function save_base_info(Request $request)
    {
        $data = $request->param();
        if($data['gender'] == 0) {
            throw new Exception('请设置你的性别');
        }
        $keys = [
            'gender',
            'birthday',
            'wxid',
            'live_province',
            'live_city',
            'birth_province',
            'birth_city',
            'marriage_status',
            'height',
            'weight',
            'education',
            'graduate_institutions',
            'job',
            'job_type',
            'annual_income',
            'house_status',
            'house_location',
            'car_status',
            'kids',
            'blood_type',
            'nation',
            'faith',
            'smoke_status',
            'drink_status',
            'distance_relationship_type',
            'child_type',
            'live_parents_type',
            'intro',
            'sms_notice_enable',
            'wechat_notice_enable',
            'hidden_mobile_enable',
            'phone_audit_enable',
            'wxid_audit_enable',
            'marriage_seeking_status',
        ];
        $data = arrayOnly($data, $keys);
        if(isset($data['birthday']) && $data['birthday'] > 0) {
            $constellations = constellations();
            $constellation = getConstellation(date('n', $data['birthday']), date('j', $data['birthday']));
            $k = 0;
            foreach ($constellations as $index => $val) {
                if($constellation == $index) {
                    $data['constellation'] = $k;
                    break;
                }
                $k++;
            }
            $data['zodiac'] = getZodiac(date('Y', $data['birthday']));
        }
        $this->memberProfileService->update(
            $data,
            ['user_id' => $this->user['id']]
        );
        $member = $this->memberProfileService->getByUserId($this->user['id']);
        $member = $this->memberProfileService->format($member);
        return $this->success(['member' => $member]);
    }

    public function save_expect_info(Request $request)
    {
        $data = $request->param();
        $keys = [
            'expect_age_from',
            'expect_age_to',
            'expect_height_from',
            'expect_height_to',
            'expect_weight_from',
            'expect_weight_to',
            'expect_location',
            'expect_marriage_status',
            'expect_house_status',
            'expect_house_location',
            'expect_car_status',
            'expect_kids_status',
            'expect_education_type',
            'expect_income_type',
            'expect_verify_status'
        ];
        $data = arrayOnly($data, $keys);
        $this->memberProfileService->update(
            $data,
            ['user_id' => $this->user['id']]
        );
        $member = $this->memberProfileService->getByUserId($this->user['id']);
        $member = $this->memberProfileService->format($member);
        return $this->success(['member' => $member]);
    }

    public function delete()
    {
        $this->memberProfileService->delete([
            'user_id' => $this->user['id']
        ]);
        return $this->success();
    }
}